package 作业二;

public class SequentailStack implements Stack{
	Object[] stack; 
	  final int defaultSize = 10; 
	  int top; 
	  int maxSize; 
	  public SequentailStack() {
		         init(defaultSize);
		   }
		   public SequentailStack(int size) {
		        init(size);
		    }
		  
   public void init(int size) {
		         this.maxSize = size;
		      top = -1;
		        stack = new Object[size];
		     }
		    public Object getTop() throws Exception {
		        if (isEmpty()) {
		            throw new Exception("堆栈为空！");
		         }
		        return stack[top];
		   }  public boolean isEmpty() {
		        return top == -1;
		     }
   public Object pop() throws Exception {
		         if (isEmpty()) {
		            throw new Exception("堆栈为空！");
		        }
		        top--;
		       return stack[top+1];
		    }
		    //进栈操作
		     @Override
		    public void push(Object obj) throws Exception {
		           if (top == maxSize-1) {
		            throw new Exception("堆栈已满！");
		        }
		        
		        top++;
		        stack[top] = obj;
		    }
			@Override
			public Object peek() throws Exception {
				// TODO Auto-generated method stub
				return null;
			}
			@Override
			public int size() {
				// TODO Auto-generated method stub
				return 0;
			}


}
